<template>
	<view>
		<!-- 搜索框 -->
		<view class="search-box">
			<uni-search-bar @confirm="search" @input="input" :radius="100" cancelButton='none'></uni-search-bar>
		</view>
		<!-- 搜索建议 -->
		<view class="search-suggest" v-show="keywords">
			<view class="search-suggest-item" v-for="(item,index) in searchResultsList" :key="index" @click="gotoGoodsDetail(item.goods_id)">
				<text class="left">{{ item.goods_name }}</text>
				<uni-icons type="arrowright" size="16" v-if="item.goods_id!==-1"></uni-icons>
			</view>
		</view>
		<!-- 搜索历史 -->
		<view class="search-history" v-show="!keywords">
			<!-- 头部区域 -->
			<view class="history-title">
				<text>搜索历史</text>
				<view class="deleteBox" v-if="isDelete">
					<uni-tag text="清空所有" @click="clearAll"></uni-tag>
					<uni-icons type="closeempty" size="17" @click="clear"></uni-icons>
				</view>
				<view class="deleteBox" v-else>
					<view class="placeholder">
					</view>
					<uni-icons type="trash" size="17" @click="clear"></uni-icons>
				</view>
			</view>
			<!-- 列表区域 -->
			<view class="history-list">
				<uni-tag :class="[isDelete?'deleteIcon':'']" :text="item" v-for="(item,index) in historyList" :key="index" @click="clearItem(item,index)"></uni-tag>
			</view>
		</view>
		<!-- 删除弹出层 -->
		<uni-popup ref="popup" type="dialog">
			<uni-popup-dialog type="input" title="提示" content="确定删除所有历史记录吗？" :duration="2000" :before-close="true" @close="close"
			 @confirm="sureClearAll"></uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	import uniPopup from '@/components/uni-popup/uni-popup.vue'
	import uniPopupMessage from '@/components/uni-popup/uni-popup-message.vue'
	import uniPopupDialog from '@/components/uni-popup/uni-popup-dialog.vue'
	export default {
		data() {
			return {
				// 输入框防抖定时器
				timer: null,
				// 输入框
				keywords: '',
				// 搜索结果列表
				searchResultsList: [],
				// 输入历史列表
				historyList: [],
				// 是否为删除状态
				isDelete: false
			};
		},
		onLoad() {
			this.historyList = JSON.parse(uni.getStorageSync('searchHistoryList') || '[]')
		},
		components: {
			uniPopup,
			uniPopupMessage,
			uniPopupDialog
		},
		methods: {
			// 输入框输入事件
			input(e) {
				// e.value 是最新的搜索内容
				clearTimeout(this.timer)
				this.timer = setTimeout(() => {
					this.keywords = e.value.trim()
					this.getSearchResult()
				}, 500)
			},
			// 获取搜索结果请求
			async getSearchResult() {
				// 非空校验
				if (this.keywords === '') {
					this.searchResultsList = []
					return
				}
				const res = await uni.$http.get('/api/public/v1/goods/qsearch', {
					query: this.keywords
				})
				if (res.data.meta.status !== 200) return uni.$showMsg()
				if (res.data.message.length === 0) {
					return this.searchResultsList = [{
						goods_name: '没有找到相关内容',
						goods_id: -1
					}]
				}
				this.searchResultsList = res.data.message
				this.saveHistoryKeyWords()
			},
			// 搜索结果点击跳转
			gotoGoodsDetail(id) {
				if (id === -1) return
				uni.navigateTo({
					url: '/subpkg/goods_detail/goods_detail?goods_id=' + id
				})
			},
			// 存储搜索历史方法
			saveHistoryKeyWords() {
				for (let i = 0; i < this.historyList.length; i++) {
					if (this.historyList[i] === this.keywords) {
						this.historyList.splice(i, 1);
						break;
					}
				}
				this.historyList.unshift(this.keywords)
				// 数据持久化存储
				uni.setStorageSync('searchHistoryList', JSON.stringify(this.historyList))
			},
			// 点击删除按钮
			clear() {
				this.isDelete = !this.isDelete
			},
			// 删除当前历史记录
			clearItem(item,index) {
				if (this.isDelete) {
					// 删除状态删除
					console.log(index)
					this.historyList = this.historyList.filter((item, i) => {
						return i !== index
					})
					uni.setStorageSync('searchHistoryList', JSON.stringify(this.historyList))
				} else {
					// 非删除状态跳转
					uni.navigateTo({
						url:'/subpkg/goods_list/goods_list?query='+item
					})
				}
			},
			// 关闭删除弹层
			close(done) {
				done()
			},
			// 删除所有历史记录
			clearAll() {
				this.$refs.popup.open()
			},
			sureClearAll(done) {
				this.historyList = [],
					uni.setStorageSync('searchHistoryList', JSON.stringify(this.historyList))
				done()
			}
		}
	}
</script>

<style lang="scss">
	.search-box {
		position: sticky;
		top: 0;
		z-index: 999;
	}

	.search-suggest {
		font-size: 24rpx;
		padding: 0 10rpx;

		.search-suggest-item {
			padding: 26rpx 0;
			display: flex;
			justify-content: space-between;
			align-items: center;
			border-bottom: 2rpx solid #efefef;

			.left {
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
				margin-right: 10rpx;
			}
		}
	}

	.search-history {
		padding: 0 10rpx;

		.history-title {
			height: 80rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			font-size: 26rpx;
			border-bottom: 2rpx solid #efefef;

			.deleteBox {
				width: 222rpx;
				font-size: 24rpx;
				display: flex;
				align-items: center;

				.uni-tag {
					margin-right: 10rpx;
				}

				.placeholder {
					width: 300rpx;
				}
			}
		}

		.history-list {
			display: flex;
			flex-wrap: wrap;

			.uni-tag {
				position: relative;
				margin-top: 15rpx;
				margin-right: 15rpx;
				box-sizing: border-box;
			}

			.deleteIcon {
				position: relative;

				// margin-top: 15rpx;
				// margin-right: 15rpx;
				// box-sizing: border-box;
				&::after {
					content: '-';
					position: absolute;
					display: block;
					top: 7.5rpx;
					right: 5rpx;
					color: #fff;
					font-size: 24rpx;
					line-height: 18rpx;
					background-color: #f00;
					width: 20rpx;
					height: 20rpx;
					text-align: center;
					border-radius: 20rpx;
					box-sizing: border-box;
					z-index: 999;
				}
			}
		}
	}
</style>
